﻿using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.admin.order
{
    public partial class tran_order : MxWeiXinPF.Web.UI.ManagePage
    {
        protected string keywords = string.Empty;
        protected int page;
        protected int pageSize;
        protected int payment_status;
        protected MxWeiXinPF.Model.manager admin_info;
        protected int totalCount;
        protected int selectshopid;
        protected string starttime;
        protected string endtime;
        protected decimal sumamount = 0.0m;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.payment_status = MXRequest.GetQueryInt("status");
            this.selectshopid = MXRequest.GetQueryInt("selectshopid");
            this.keywords = MXRequest.GetQueryString("keywords");
            this.pageSize = this.GetPageSize(10);
            this.starttime = MXRequest.GetQueryString("start");
            this.endtime = MXRequest.GetQueryString("end");
            if (!this.Page.IsPostBack)
            {
                base.ChkAdminLevel("tran_order", MXEnums.ActionEnum.View.ToString());
                this.admin_info = base.GetAdminInfo();
                this.RptBind(" " + this.CombSqlTxt(this.payment_status, this.selectshopid, this.keywords), "create_time desc");
                if (string.IsNullOrEmpty(this.starttime))
                {
                    txtStart.Text = DateTime.Now.AddDays(-7).ToShortDateString();
                }
                else
                {
                    this.txtStart.Text = starttime;
                }
                if (string.IsNullOrEmpty(endtime))
                {
                    txtEnd.Text = DateTime.Now.AddDays(1).ToShortDateString();
                }
                else
                {
                    this.txtEnd.Text = endtime;
                }
            }
        }

        protected string CombSqlTxt(int _payment_status, int selectshopid, string _keywords)
        {
            StringBuilder builder = new StringBuilder();
            if (_payment_status > 0)
            {
                builder.Append(" and status=" + _payment_status);
            }
            if (this.selectshopid == 0) //店铺全选状态
            {
                Model.manager weixin = base.GetAdminInfo();
                if (weixin.agentLevel == 0)  //普通店铺
                {
                    int tempshopid = 0;
                    if (GetRelativeAdminInfo() != null)
                    {
                        tempshopid = GetRelativeAdminInfo().id;
                    }
                    else
                    {
                        tempshopid = GetAdminInfo().id;
                    }

                    if (weixin.id == 39 || tempshopid == 39) //管理员
                    {

                    }
                    else //普通店铺
                    {
                        builder.AppendFormat(" and shopid =" + tempshopid);
                    }
                }
                else //代理店铺
                {

                    builder.Append(" and shopid in (");
                    foreach (ListItem x in this.ddlShops.Items)
                    {
                        if (x.Value != "0")
                        {
                            builder.Append(x.Value);
                            builder.Append(",");
                        }
                    }
                    builder = builder.Remove(builder.Length - 1, 1);
                    builder.Append(") ");
                    // strTemp.AppendFormat(" shopid =" + selectshopid);
                }
            }
            else
            {
                builder.AppendFormat(" and shopid =" + selectshopid);
            }
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                builder.Append(" and (real_name like '%" + _keywords + "%' or user_name like '%" + _keywords + "%')");
            }

            if (string.IsNullOrEmpty(starttime))
            {
                builder.AppendFormat(" and create_time >= '" + DateTime.Now.AddDays(-7).ToShortDateString() + "' ");
            }
            else
            {
                builder.AppendFormat(" and create_time >= '" + starttime + "' ");
            }
            if (string.IsNullOrEmpty(endtime))
            {
                builder.AppendFormat(" and create_time <= '" + DateTime.Now.AddDays(1).ToShortDateString() + "' ");
            }
            else
            {
                builder.AppendFormat(" and create_time <= '" + endtime + "' ");
            }

            return builder.ToString();
        }

        protected void ddlPaymentStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("tran_order.aspx", "status={0}&selectshopid={1}&keywords={2}&start={3}&end={4}", new string[] { this.ddlPaymentStatus.SelectedValue, this.selectshopid.ToString(), this.keywords, this.starttime, this.endtime }));
        }

        protected void ddlShops_SelectedIndexChanged(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("tran_order.aspx", "status={0}&selectshopid={1}&keywords={2}&start={3}&end={4}", new string[] { this.payment_status.ToString(), this.ddlShops.SelectedValue, this.keywords, this.starttime, this.endtime }));
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.starttime = this.txtStart.Text.ToString();
            this.endtime = this.txtEnd.Text.ToString();
            base.Response.Redirect(Utils.CombUrlTxt("tran_order.aspx", "status={0}&selectshopid={1}&keywords={2}&start={3}&end={4}", new string[] { this.payment_status.ToString(), this.selectshopid.ToString(), this.txtKeywords.Text, this.starttime, this.endtime }));
        }
        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("tran_order_shop_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }

        private void RptBind(string _strWhere, string _orderby)
        {
            MxWeiXinPF.Model.manager weixin = base.GetAdminInfo();
            this.page = MXRequest.GetQueryInt("page", 1);
            if (this.payment_status > 0)
            {
                this.ddlPaymentStatus.SelectedValue = this.payment_status.ToString();
            }
            if (this.selectshopid > 0)
            {
                this.ddlShops.SelectedValue = this.selectshopid.ToString();
            }
            else
            {
                this.ddlShops.SelectedValue = "0";
            }
            this.txtKeywords.Text = this.keywords;
            BLL.dt_transfer bll = new BLL.dt_transfer();
            DataSet set = new DataSet();
            DataSet ds2 = new DataSet();
            set = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);

            ds2 = bll.GetListSum(_strWhere);
            if (((set != null) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
            {
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    DataRow row = set.Tables[0].Rows[i];
                    row["statusName"] = this.GetOrderStatus(MyCommFun.Obj2Int(row["status"]));
                }
            }
            this.rptList.DataSource = set;
            this.rptList.DataBind();
            this.TreeBind();
            this.txtPageNum.Text = this.pageSize.ToString();
            string linkUrl = Utils.CombUrlTxt("tran_order.aspx", "status={0}&selectshopid={1}&keywords={2}&page={3}&start={4}&end={5}", new string[] { this.payment_status.ToString(), this.selectshopid.ToString(), this.keywords, "__id__", this.starttime, this.endtime });
            this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
            if (ds2 != null && ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
            {
                if (ds2.Tables[0].Rows[0][0] != null && ds2.Tables[0].Rows[0][0].ToString() != "")
                {
                    sumamount = Convert.ToDecimal(ds2.Tables[0].Rows[0][0].ToString());
                    this.lbSum.Text = sumamount.ToString() + "元";
                }
            }
            this.lbSum.Text = sumamount.ToString() + "元";
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
            {
                Utils.WriteCookie("tran_order_shop_page_size", num.ToString(), 0x3840);
            }
            base.Response.Redirect(Utils.CombUrlTxt("tran_order.aspx", "status={0}&selectshopid={1}&keywords={2}&start={3}&end={4}", new string[] { this.payment_status.ToString(), this.selectshopid.ToString(), this.keywords, this.starttime, this.endtime }));
        }

        protected string GetOrderStatus(int payment_status)
        {
            string str = "已打款";
            if (payment_status == 0)
            {
                str = "未打款";
            }    
            return str;
        }

        private void TreeBind()
        {
            try
            {
                BLL.manager bman = new BLL.manager();
                DataSet ds = bman.GetList(-1, " id<> 39 ", "");
                this.ddlShops.Items.Clear();
                this.ddlShops.Items.Add(new ListItem("所有店铺", "0"));
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    if (row["id"].ToString() == this.selectshopid.ToString())
                    {
                        ddlShops.SelectedValue = row["id"].ToString();
                    }
                    this.ddlShops.Items.Add(new ListItem(row["real_name"].ToString(), row["id"].ToString()));
                }
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    if (row["id"].ToString() == this.selectshopid.ToString())
                    {
                        ddlShops.SelectedValue = row["id"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

    }
}